/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package co.edu.senaclem.persistencia;

import co.edu.senaclem.modelo.Area;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author DILOVE
 */
@Stateless
public class AreaDAO implements IAreaDAO{

    @PersistenceContext
    private EntityManager entityManager;
    
    @Override
    public Area consultarPorId(int codigo) throws Exception {
        try {
            return entityManager.find(Area.class, codigo);
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public List<Area> consultar() throws Exception {
        try {
             String jpql="Select a From Area a";
             Query query = entityManager.createQuery(jpql);
             return query.getResultList();
        } catch (RuntimeException e) {
            throw e;
        }
    }
    
    @Override
    public List<Area> consultar(Integer dependencia) throws Exception {
        try {
            String jpql="Select a From Area a where a.codigodependencia.codigodependencia="+dependencia;
            Query query = entityManager.createQuery(jpql);
            return query.getResultList();
        } catch (RuntimeException e) {
            throw e;
        }
    }
    
}
